home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / MyDeviceLoop / Source / MyDeviceLoop.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.9 KB  |  206 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    MyDeviceLoop                                            */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows how to write a device loop that        */
  6. /*                    works under System 7 and pre-7.0 systems.  As            */
  7. /*                    described on pages 21-23 and 21-24 of Inside Mac        */
  8. /*                    volume VI, a device loop procedure searches all            */
  9. /*                    active screen devices, calling a drawing procedure        */
  10. /*                    whenever it encounters a screen that intersects            */
  11. /*                    the drawing region.  In this app the drawing            */
  12. /*                    region is the app's window bounds and the chosen        */
  13. /*                    drawing procedure simply displays the screen's            */
  14. /*                    colors for every device the window bounds intersect.    */
  15. /*                                                                            */
  16. /*    Files:            MyDeviceLoop.π                                            */
  17. /*                    MyDeviceLoop.c                                            */
  18. /*                                                                            */
  19. /*    Programmer:        Edgar Lee                                                */
  20. /*    Organization:    Apple Computer, Inc.                                    */
  21. /*    Department:        Developer Technical Support, DTS                        */
  22. /*    Language:        C (Think C version 5.0.1)                                */
  23. /*    Date Created:    02-21-92                                                */
  24. /*                                                                            */
  25. /****************************************************************************/
  26.  
  27. #include <Dialogs.h>
  28. #include <Fonts.h>
  29.  
  30. /* Constant Declarations */
  31.  
  32. #define    WWIDTH        400
  33. #define    WHEIGHT        256
  34.  
  35. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  36. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  37.  
  38.  
  39. enum PointSelector {topLeft,
  40.             botRight};
  41. /* Global Variable Definitions */
  42.  
  43. WindowPtr    gWindow;
  44.  
  45. void initMac();
  46. void createWindow();
  47. void doMyDeviceLoop();
  48. void doDraw();
  49. void doEventLoop();
  50.  
  51. void main(void)
  52. {
  53.     initMac();
  54.     
  55.     createWindow();
  56.  
  57.     doEventLoop();
  58. }
  59.  
  60. void initMac()
  61. {
  62.     MaxApplZone();
  63.  
  64.     InitGraf( &qd.thePort );
  65.     InitFonts();
  66.     InitWindows();
  67.     InitMenus();
  68.     TEInit();
  69.     InitDialogs( nil );
  70.     InitCursor();
  71.     FlushEvents( 0, everyEvent );
  72. }
  73.  
  74. void createWindow()
  75. {
  76.     Rect rect;
  77.     
  78.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  79.     gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc,
  80.                             (WindowPtr)-1L, true, 0L );                        
  81.     SetPort( gWindow );
  82.     
  83.     TextFont( times );
  84.     TextSize( 48 );
  85.     TextMode( srcCopy );
  86. }
  87.  
  88. void doMyDeviceLoop()
  89. {
  90.     int            depth;
  91.     Rect        gDeviceRect;
  92.     Rect        intersectingRect;
  93.     GDHandle    gDevice;
  94.     //Point        point;
  95.     
  96.     /* Get the handle to the first device in the list. */
  97.     gDevice = GetDeviceList();
  98.     
  99.     /* Loop through all the devices in the list. */
  100.     while (gDevice != nil)
  101.     {
  102.         /* Get the device's gdRect and convert it to local coordinates. */
  103.         gDeviceRect = (**gDevice).gdRect;
  104.         depth = (**(**gDevice).gdPMap).pixelSize;
  105.             
  106.         GlobalToLocal( topLeft );
  107.         GlobalToLocal( (Point *)botRight );
  108.         
  109.         /* Check if the app's window rect intersects the device's, and if it */
  110.         /*    does, set the clip region's rect to the intersection, then DRAW! */
  111.         
  112.         if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect ))
  113.         {
  114.             ClipRect( &intersectingRect );
  115.             doDraw( depth, &intersectingRect );
  116.         }
  117.         
  118.         /* Get the next device in the list. */
  119.         gDevice = GetNextDevice( gDevice );
  120.     }
  121. }
  122.  
  123. void doDraw( depth)
  124. int        depth;
  125. {
  126.     int                i;
  127.     int                totalColors;
  128.     int                penThickness;
  129.     //RGBColor        color;
  130.     CTabHandle        ctable;
  131.     Str255            string = "\pDirect Colors";
  132.     
  133.     if (depth > 8)
  134.     {
  135.         BackColor( blackColor );
  136.         ForeColor( blueColor );
  137.         
  138.         /* Draw text for direct colors mode. */
  139.         EraseRect( &gWindow->portRect );
  140.         MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 );
  141.         DrawString( string );
  142.     }
  143.     else
  144.     {
  145.         /* Get the colortable at this depth. */
  146.         ctable = GetCTable( depth );
  147.         
  148.         /* Set the line thickness to a fraction of the window height. */
  149.         totalColors = (2 << depth) / 2;
  150.         penThickness = gWindow->portRect.bottom / totalColors;
  151.         PenSize( 1, penThickness );
  152.     
  153.         /* Now draw the colors at this depth. */
  154.         for (i = 0; i < totalColors; i++)
  155.         {
  156.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  157.             MoveTo( 0, i * penThickness );
  158.             LineTo( gWindow->portRect.right, i * penThickness );
  159.         }
  160.         
  161.         /* Release the colortable memory. */
  162.         DisposeCTable( ctable );
  163.     }
  164. }
  165.  
  166. void doEventLoop()
  167. {
  168.     EventRecord event;
  169.     WindowPtr   window;
  170.     short       clickArea;
  171.     Rect        screenRect;
  172.  
  173.     for (;;)
  174.     {
  175.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  176.         {
  177.             if (event.what == mouseDown)
  178.             {
  179.                 clickArea = FindWindow( event.where, &window );
  180.                 
  181.                 if (clickArea == inDrag)
  182.                 {
  183.                     screenRect = (**GetGrayRgn()).rgnBBox;
  184.                     DragWindow( window, event.where, &screenRect );
  185.                 }
  186.                 else if (clickArea == inContent)
  187.                 {
  188.                     if (window != FrontWindow())
  189.                         SelectWindow( window );
  190.                 }
  191.                 else if (clickArea == inGoAway)
  192.                     if (TrackGoAway( window, event.where ))
  193.                         return;
  194.             }
  195.             else if (event.what == updateEvt)
  196.             {
  197.                 window = (WindowPtr)event.message;    
  198.                 SetPort( window );
  199.                 
  200.                 BeginUpdate( window );
  201.                 doMyDeviceLoop();
  202.                 EndUpdate( window );
  203.             }
  204.         }
  205.     }
  206. }